import React from "react";
// v6里面已经没有useHistory了, 取而代之的是useNavigate
import { Link, Routes, Route, useNavigate } from "react-router-dom";

const Home = () => {
  // 执行useNavigate函数。返回的是一个方法
  const navigate = useNavigate();
  const fn = () => {
    // navigate方法直接传入路径，相当于push方法
    // navigate("/about");

    // 如果想要做replace的效果，需要加入第二个参数{ replace: true }
    // navigate("/about", { replace: true });

    // 如果想做前进后退的，那么里面直接传入数字
    navigate(-1);
  };

  return (
    <>
      <div>这是home页面</div>
      <button onClick={fn}>btn</button>
    </>
  );
};

const About = () => {
  return <div>这是about页面</div>;
};

const App = () => {
  return (
    <>
      <h2>react-router-v6-编程式导航</h2>
      <ul>
        <li>
          <Link to="/home">home</Link>
        </li>
        <li>
          <Link to="/about">about</Link>
        </li>
      </ul>

      <hr />

      <Routes>
        {/* 404写法几乎一样，写在最下面 */}
        <Route path="/home" element={<Home />}></Route>
        <Route path="/about" element={<About />}></Route>
        <Route path="*" element={<div>404 font found...</div>}></Route>
      </Routes>
    </>
  );
};

export default App;
